<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/1/7
 * Time: 15:07
 */

namespace app\admin\controller;


use think\Db;

class Getdraw extends Base
{
    /**
     * 首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $start_time = input('start_time');
            $end_time = input('end_time');
            $user_phone = input('user_phone');
            
            $where['info_times_type'] = 1;
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == '')
            {
                $where['info_time'] = ['>= time', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != '')
            {
                $where['info_time'] = ['<= time', $end_time];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != '')
            {
                $where['info_time'] = ['between time', [$start_time, $end_time]];
            }
            
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('draw_info')
                ->join('up_u_user', 'user_id = info_user_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $count2 = Db::name('draw_info')
                ->join('up_u_user', 'user_id = info_user_id', 'LEFT')
                ->where($where)
                ->column('info_id');// 获取总条数
            $lists = Db::name('draw_info')
                ->join('up_u_user', 'user_id = info_user_id', 'LEFT')
                ->field('info_id, info_user_id, user_username, user_phone, info_district, info_draw_times, 
                info_times_desc, info_time')
                ->where($where)
                ->order('info_time desc')
                ->page($NowPage, $limits)
                ->select();
            $str = '';
            if($count2){
                $str = implode($count2,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count2' => $str, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $info_ids = input('ids');
        if($info_ids == '') return json(['code' => 100, 'msg' => '请选择需要导出的数据']);
        $lists = Db::name('draw_info')
            ->join('up_u_user', 'user_id = info_user_id', 'LEFT')
            ->field('info_id, info_user_id, user_username, user_phone, info_district, info_draw_times, 
                info_times_desc, info_time')
            ->where('info_id', 'IN', $info_ids)
            ->order('info_time desc')
            ->select();
        
        foreach($lists as $k => $v){
            // 处理特殊字符串
            $user_name = json_encode($v['user_username']);
            $name = preg_replace("/\\\u[ed][0-9a-f]{3}\\\u[ed][0-9a-f]{3}/", "*", $user_name);//将特殊字符串替换成*
            $lists[$k]['user_username'] = json_decode($name);
            // 手机号
            $lists[$k]['user_phone'] = "\t".$v['user_phone']."\t";
        }
    
        $cellname = [
            ['info_user_id', '用户ID', 15, 'LEFT'],
            ['user_username', '昵称', 15, 'LEFT'],
            ['user_phone', '手机号', 20, 'LEFT'],
            ['info_district', '所在区域', 20, 'LEFT'],
            ['info_times_desc', '获取途径', 20, 'LEFT'],
            ['info_draw_times', '获取次数', 20, 'LEFT'],
            ['info_time', '获取时间', 20, 'LEFT'],
        ];
        $res = exportExcel('兑换记录','Draw',$cellname,$lists);
        return json($res);
    }
}